package com.lbssoft.gpsbd.service.impl;

import com.google.common.base.Strings;
import com.lbssoft.gpsbd.common.Utils;
import com.lbssoft.gpsbd.dao.DeviceGeofenceMapper;
import com.lbssoft.gpsbd.dao.GeofenceMapper;
import com.lbssoft.gpsbd.model.Geofence;
import com.lbssoft.gpsbd.model.GeofencePermission;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.model.vo.GeofenceDevicesParam;
import com.lbssoft.gpsbd.model.vo.UserVo;
import com.lbssoft.gpsbd.service.GeofenceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by nczc on 2018/8/17.
 */
@Service
public class GeofenceServiceImpl implements GeofenceService {

    @Autowired
    GeofenceMapper geofenceMapper;

    @Autowired
    DeviceGeofenceMapper deviceGeofenceMapper;

    @Override
    public Result add(Geofence geofence) {
        Result result = new Result();
        UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        if(Strings.isNullOrEmpty(geofence.getUuid())){
            geofence.setUuid("test-"+ Utils.getUUID().substring(5));
        }
        geofenceMapper.add(geofence);

        GeofencePermission geofencePermission = new GeofencePermission();
        geofencePermission.setGeofenceId(geofence.getId());
        geofencePermission.setUserId(user.getId());
        geofenceMapper.addUserGeofence(geofencePermission);
        result.setCode(0);
        result.setData(geofence);
        return result;
    }

    @Override
    public Result update(Geofence geofence) {

        Result result = new Result();
        geofenceMapper.update(geofence);
        result.setCode(0);
        return result;
    }

    @Override
    public Result delete(long id) {
        Result result = new Result();
        geofenceMapper.delete(id);
        result.setData(id);
        return result;
    }

    @Override
    public Result list() {
        Result result = new Result();
        UserVo user = (UserVo)SecurityUtils.getSubject().getPrincipal();
        List<Geofence> geofences = geofenceMapper.getGeofences(user.getId());

        result.setCode(0);
        result.setData(geofences);
        return result;
    }

    @Override
    public Result bindDevices(GeofenceDevicesParam param) {
        Result result = new Result();
        //todo 需要同步到采集中心
        for(Integer deviceId:param.getDeviceIds()){
            deviceGeofenceMapper.addDeviceGeofence(deviceId, param.getGeofenceId());
        }
        return result;
    }

    @Override
    public Result unbindDevices(GeofenceDevicesParam param) {
        Result result = new Result();
        //todo 需要同步到采集中心
        for(Integer deviceId:param.getDeviceIds()){
            deviceGeofenceMapper.delete(deviceId, param.getGeofenceId());
        }
        return result;
    }

    @Override
    public Result getDeviceIds(long id) {
        Result result = new Result();
        List<Integer> deviceIds = deviceGeofenceMapper.getDeviceIds(id);
        result.setCode(0);
        result.setData(deviceIds);
        return result;
    }
}
